<template>
  <div class="app-container">
    <el-row>
      <el-col :span="16">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scxx:process:add']"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scxx:process:edit']"
        >修改</el-button>
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['scxx:process:remove']"-->
<!--        >删除</el-button>-->
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scxx:process:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="isUse">
            <el-select v-model="queryParams.isUse" placeholder="请选择是否启用" clearable>
              <el-option
                v-for="dict in dict.type.is_use"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="gxName">
            <el-input
              v-model="queryParams.gxName"
              placeholder="请输入工序名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工序编码" align="center" prop="gxCode" />
      <el-table-column label="工序名称" align="center" prop="gxName" />
      <el-table-column label="工序类型" align="center" prop="gxTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gx_type" :value="scope.row.gxTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="待工时间" align="center" prop="waitTime" />
      <el-table-column label="标准工时" align="center" prop="niceTime" />
      <el-table-column label="报工方式" align="center" prop="bgTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bg_type" :value="scope.row.bgTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="检验标准" align="center" prop="checkName" />
      <el-table-column label="是否启用" align="center" prop="isUse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.isUse"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:process:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:process:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="gxName">
              <el-input v-model="form.gxName" placeholder="请输入工序名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序类型" prop="gxTypecode">
              <el-select v-model="form.gxTypecode" placeholder="请选择工序类型">
                <el-option
                  v-for="dict in dict.type.gx_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="待工时间" prop="waitTime">
              <el-input v-model="form.waitTime" placeholder="请输入待工时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准工时" prop="niceTime">
              <el-input v-model="form.niceTime" placeholder="请输入标准工时" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报工方式" prop="bgTypecode">
              <el-select v-model="form.bgTypecode" placeholder="请选择报工方式">
                <el-option
                  v-for="dict in dict.type.bg_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select v-model="form.isUse" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in dict.type.is_use"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="检验标准" prop="checkCode">
          <el-input v-model="form.checkCode" placeholder="请输入检验标准编码" readonly
                    @click.native="dialogVisible=true;getJybzList()" suffix-icon="el-icon-more"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      title="选择模版"
      :visible.sync="dialogVisible"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectJybzWithName"
                        v-model="selectJybz.jybzName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectJybzWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectJybz.JybzName='';getJybzList();" type="info">重置</el-button>
            </el-col>
          </el-row>

        </el-header>
        <el-main>

          <!-- 展示产品分类信息 -->
          <el-table
            :data="jyData"
            height="250"
            border
            style="width: 100%"
            @row-click="getJyData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="mbCode"
              label="模板编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="mbName"
              label="模版名称"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="jybzTotal>0"
            :total="jybzTotal"
            :page.sync="selectJybz.pageNum"
            :limit.sync="selectJybz.pageSize"
            @pagination="getJybzList()"
          />
        </el-main>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/scxx/process";
import { listStandard } from "@/api/scxx/standard";
import { getGxWithGxCode, delGyRoadAndGx} from "@/api/scxx/gyRoadConfig";

export default {
  name: "Process",
  dicts: ['bg_type', 'is_use', 'gx_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序定义表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gxName: null,
        gxTypecode: null,
        waitTime: null,
        niceTime: null,
        bgTypecode: null,
        checkCode: null,
        isUse: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gxName: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
        gxTypecode: [
          { required: true, message: "工序类型不能为空", trigger: "change" }
        ],
        waitTime: [
          { required: true, message: "待工时间不能为空", trigger: "blur" }
        ],
        niceTime: [
          { required: true, message: "标准工时不能为空", trigger: "blur" }
        ],
        bgTypecode: [
          { required: true, message: "报工方式不能为空", trigger: "change" }
        ],
        checkCode: [
          { required: true, message: "检验标准编码不能为空", trigger: "blur" }
        ],
        isUse: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      },
      // 选择检验标准的弹出层
      dialogVisible: false,
      // 检验标准信息
      jyData:[],
      // 检验标准搜索值
      selectJybz: {
        pageNum: 1,
        pageSize: 10,
        jybzName: "",
      },
      // 检验标准总条数
      jybzTotal:0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 根据产品类型名模糊查询
    selectJybzWithName(){
      if (this.selectJybz.jybzName.trim() != "" ){
        this.getJybzList();
      }
    },
    // 获取产品分类选择的数据
    getJyData(row, column, event){
      this.form.checkCode = row.mbCode
      this.dialogVisible = false
    },
    // 根据条件查询产品分类
    getJybzList(){
      listStandard(this.selectJybz).then(response => {
        this.jyData = response.rows;
        this.jybzTotal = response.total;
      });
    },
    /** 查询工序定义列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gxCode: null,
        gxName: null,
        gxTypecode: null,
        waitTime: null,
        niceTime: null,
        bgTypecode: null,
        checkCode: null,
        isUse: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gxCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gxCode = row.gxCode || this.ids
      getProcess(gxCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工序定义";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gxCode != null) {
            updateProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let that = this;
      const gxCodes = row.gxCode || this.ids;
      this.$modal.confirm('是否确认删除工序定义编号为"' + gxCodes + '"的数据项？').then(function() {
        getGxWithGxCode(gxCodes).then(response => {
          if (response.rows.length > 0){
            that.$modal.confirm('将同步删除工艺路线的绑定项').then(function() {
              const param = {
                gxCode : gxCodes
              }
              delGyRoadAndGx(param)
            })
          }
          delProcess(gxCodes)
          that.getList();
          that.$modal.msgSuccess("删除成功");
        });
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scxx/process/export', {
        ...this.queryParams
      }, `process_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scope lang="scss">
.yu {
  .el-form-item__content {
    display: flex;
  }
}
</style>
